﻿using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gadgeteer.Interfaces.Sample.Safe.SafeApp.Test
{
    [TestFixture]
    public class LockTest
    {
        [Test]
        public void Unlock_With_CorrectCode()
        {
            var padlock = new Lock(new [] { 1, 2, 1});
            Assert.That(padlock.IsOpen, Is.False);
            int openReceived = 0;
            padlock.Opened += (s, e) => { openReceived++; };

            padlock.OpenWithCode(new[] { 1, 2, 1 });
            Assert.That(padlock.IsOpen, Is.True);
            Assert.That(openReceived, Is.EqualTo(1));
        }

        [Test]
        public void Fail_With_InCorrectCode()
        {
            var padlock = new Lock(new[] { 1, 2, 1 });
            Assert.That(padlock.IsOpen, Is.False);

            padlock.OpenWithCode(new[] { 2, 1, 2 });
            Assert.That(padlock.IsOpen, Is.False);
        }

        [Test]
        public void Fail_With_ShortKey()
        {
            var padlock = new Lock(new[] { 1, 2, 1 });
            Assert.That(padlock.IsOpen, Is.False);

            padlock.OpenWithCode(new[] { 0, 2, });
            Assert.That(padlock.IsOpen, Is.False);
        }

        [Test]
        public void Fail_With_ToLongKey()
        {
            var padlock = new Lock(new[] { 1, 2, 1 });
            Assert.That(padlock.IsOpen, Is.False);

            padlock.OpenWithCode(new[] { 0, 1, 0, 2 });
            Assert.That(padlock.IsOpen, Is.False);
        }

        [Test]
        public void CloseOnFailedCode()
        {
            var code = new[] { 1, 2, 1 };
            var padlock = new Lock(code);
            int closedReceived = 0;
            padlock.Closed += (s, e) => { closedReceived++; };

            Assert.That(padlock.IsOpen, Is.False);

            padlock.OpenWithCode(code);
            Assert.That(padlock.IsOpen, Is.True);

            padlock.OpenWithCode(new[] { 0, 0, 0, 0, 0 });
            Assert.That(padlock.IsOpen, Is.False);
            Assert.That(closedReceived, Is.EqualTo(1));
        }
    }
}
